﻿Imports System.Net.Mail
Imports System.Net.Mime
Imports System.Net
Public Class CADASTRO
    Inherits System.Windows.Forms.Form
    Private m_htmlDoc As mshtml.IHTMLDocument2
    Private m_arAttachment As ArrayList = New ArrayList
    Private m_arCharset(27, 1) As String
    Dim i As Integer = 0
    Dim htmleditor As New System.Windows.Forms.WebBrowser
    Private Sub LISTAITENSCONCE(ByVal path As String)
        Try
            If IO.File.Exists(path) Then
                Using sr As New IO.StreamReader(path)
                    While Not sr.EndOfStream
                        CONCESSTELComboBox.Items.Add(sr.ReadLine)
                    End While
                End Using
            Else
                MsgBox("Oooops, Arquivo não encontrado ou diretório inválido" & path.ToString)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub LISTAITENSMOD(ByVal path As String)
        Try
            If IO.File.Exists(path) Then
                Using sr As New IO.StreamReader(path)
                    While Not sr.EndOfStream
                        MODELOComboBox.Items.Add(sr.ReadLine)
                    End While
                End Using
            Else
                MsgBox("Oooops, Arquivo não encontrado ou diretório inválido" & path.ToString)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub LISTAITENCLI(ByVal path As String)
        Try
            If IO.File.Exists(path) Then
                Using sr As New IO.StreamReader(path)
                    While Not sr.EndOfStream
                        CLIENTEComboBox.Items.Add(sr.ReadLine)
                    End While
                End Using
            Else
                MsgBox("Oooops, Arquivo não encontrado ou diretório inválido" & path.ToString)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub CADASTRO_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.Enter
                SendKeys.Send("{Tab}")
        End Select
    End Sub
    Private Sub CADASTRO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.REVISAOTableAdapter.Fill(Me.REVISAODataSet.REVISAO)
        Try
            LISTAITENSCONCE("\\SERVIDOR\Funil de Vendas\Controles\CONCESSIONARIA.txt")
        Catch EX As Exception
            MsgBox("ERRO AO PREECHER COMBOBOX COM A CONCESSIONÁRIA PORÉM NÃO IMPACTUA NO FUNCIONAMENTO DO PROGRAMA")
        End Try
        Try
            LISTAITENCLI("\\SERVIDOR\Funil de Vendas\Controles\CLIENTE.txt")
        Catch EX As Exception
            MsgBox("ERRO AO PREECHER COMBOBOX COM A CONCESSIONÁRIA PORÉM NÃO IMPACTUA NO FUNCIONAMENTO DO PROGRAMA")
        End Try
        Try
            LISTAITENSMOD("\\SERVIDOR\Funil de Vendas\Controles\MODELO.txt")
        Catch EX As Exception
            MsgBox("ERRO AO PREECHER COMBOBOX COM O MODELO PORÉM NÃO IMPACTUA NO FUNCIONAMENTO DO PROGRAMA")
        End Try
        REVISAOTableAdapter.FillByAGENDAMENTO(REVISAODataSet.REVISAO, DateValue(Now).AddDays(1))
        If REVISAODataSet.Tables(0).Rows.Count > 0 Then
            MsgBox("EXISTE(M) " & REVISAODataSet.Tables(0).Rows.Count & " AGENDAMENTO(S) ATRASADO(S) ")
            PLACAMaskedTextBox.Focus()
        Else
            MsgBox("NÃO EXISTE(M) PENDÊNCIA(S)")
            PLACAMaskedTextBox.Focus()
        End If
    End Sub
    Private Sub htmlEditor_Navigated(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs)
        Dim s As New System.Text.StringBuilder()
        m_htmlDoc.body.innerHTML = s.ToString()
    End Sub
    Private Sub EnviarEMail()
        m_htmlDoc = CType(htmleditor.Document.DomDocument, mshtml.IHTMLDocument2)
        m_htmlDoc.designMode = "on"
        Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
        message.To.Add("luizpaulo@versarentacar.com.br")
        message.To.Add("lucas@versarentacar.com.br")
        message.To.Add("hugosantana@versarentacar.com.br")
        message.To.Add("cristiano@versarentacar.com.br")
        message.To.Add("plinio@versarentacar.com.br")
        message.To.Add("luizclaudio@versarentacar.com.br")
        message.From = New System.Net.Mail.MailAddress("luizpaulo@versarentacar.com.br", "Agendamento | Versa Rent a Car")
        message.Subject = "Agendamento de revisão - " & PLACAMaskedTextBox.Text & " - " & CLIENTEComboBox.Text
        message.IsBodyHtml = True
        'Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString(m_htmlDoc.body.innerHTML & "<br><img src=cid:companylogo>", Nothing, "text/plain")
        Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString(m_htmlDoc.body.innerHTML, Nothing, "text/html")
        'Dim logo As New LinkedResource(assin)
        'logo.ContentId = "assinatura"
        'htmlView.LinkedResources.Add(logo)
        message.AlternateViews.Add(htmlView)
        'message.AlternateViews.Add(plainView)
        Dim smtp As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
        smtp.Host = "smtp.versarentacar.com.br"
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential("luizpaulo@versarentacar.com.br", "VLP@072")
        Try
            smtp.Send(message)
            MessageBox.Show("E-mail enviado com sucesso!")
            Cursor.Current = Cursors.Default
        Catch ex As Exception
            MessageBox.Show("Ocorreu um erro favor verificar os dados digitados " & ex.ToString)
        End Try
    End Sub
    Private Sub PRIMEIRO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRIMEIRO.Click
        REVISAOBindingSource.MoveFirst()
    End Sub
    Private Sub VOLTAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VOLTAR.Click
        REVISAOBindingSource.MovePrevious()
    End Sub
    Private Sub NOVO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NOVO.Click
        REVISAOBindingSource.AddNew()
    End Sub
    Private Sub DELETAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DELETAR.Click
        Try
            If MsgBox("DESEJA DELETAR O REGISTRO?", MsgBoxStyle.OkCancel, "ATENÇÃO") = MsgBoxResult.Ok Then
                Me.Validate()
                REVISAOBindingSource.RemoveCurrent()
                REVISAOTableAdapter.Update(REVISAODataSet.REVISAO)
            End If
        Catch EX As Exception
            MsgBox("ERRO AO DELETAR O REGISTRO " & EX.ToString)
        End Try
    End Sub
    Private Sub FRENTE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FRENTE.Click
        REVISAOBindingSource.MoveNext()
    End Sub
    Private Sub ULTIMO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ULTIMO.Click
        REVISAOBindingSource.MoveLast()
    End Sub
    Private Sub SALVAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SALVAR.Click
        If IDTextBox.Text = "" Then
            MsgBox("FAVOR CRIAR UM NOVO REGISTRO", MsgBoxStyle.Information, "ATENÇÃO")
        Else
            If RichTextBox1.Text = "" Then
                OBSRichTextBox.Text = OBSRichTextBox.Text
            Else
                OBSRichTextBox.Text = OBSRichTextBox.Text + "EM " + DateValue(Now) + " POR " + LoginForm1.USUARIOTextBox.Text + Environment.NewLine + RichTextBox1.Text + Environment.NewLine
            End If
            htmleditor.Navigate("about:blank")
            If DateAndTime.Now.Hour < 12 Then
                Dim TEXTO As String = "Bom dia " & ",<BR><BR>Segue abaixo dados do agendamento: <BR><BR>Placa - " & PLACAMaskedTextBox.Text & "<br><br>Modelo - " & MODELOComboBox.Text & "<br><br>Concessionária - " & CONCESSTELComboBox.Text & "<br><br>Agendado para - " & AGENDAMENTOMaskedTextBox.Text & "<br><br>Cidade - " & CIDADEAGENDATextBox.Text & "<br><br>Para o cliente - " & CLIENTEComboBox.Text & "<br><br>OBS: <br>" & OBSRichTextBox.Text & "<BR></BR> STATUS DA SITUAÇÃO: " & STATUSComboBox.Text
                htmleditor.Document.Write(TEXTO)
            Else
                Dim TEXTO As String = "Boa Tarde " & ",<BR><BR>Segue abaixo dados do agendamento: <BR><BR>Placa - " & PLACAMaskedTextBox.Text & "<br><br>Modelo - " & MODELOComboBox.Text & "<br><br>Concessionária - " & CONCESSTELComboBox.Text & "<br><br>Agendado para - " & AGENDAMENTOMaskedTextBox.Text & "<br><br>Cidade - " & CIDADEAGENDATextBox.Text & "<br><br>Para o cliente - " & CLIENTEComboBox.Text & "<br><br>OBS: <br>" & OBSRichTextBox.Text & "<BR></BR> STATUS DA SITUAÇÃO: " & STATUSComboBox.Text
                htmleditor.Document.Write(TEXTO)
            End If
            m_htmlDoc = CType(htmleditor.Document.DomDocument, mshtml.IHTMLDocument2)
            m_htmlDoc.designMode = "on"
            If MsgBox("Deseja enviar email do agendamento?", MsgBoxStyle.YesNo, "ATENÇÃO") = MsgBoxResult.Yes Then
                EnviarEMail()
            End If
            Try
                Me.Validate()
                REVISAOBindingSource.EndEdit()
                TableAdapterManager.UpdateAll(REVISAODataSet)
                REVISAOTableAdapter.FillByPLACA(REVISAODataSet.REVISAO, PLACAMaskedTextBox.Text)
                RichTextBox1.Text = ""
            Catch EX As Exception
                MsgBox("ERRO NA ATUALIZAÇÃO DOS DADOS " & EX.Message)
            End Try
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If PLACAMaskedTextBox.Text = "   -   " Then
            MsgBox("FAVOR DIGITAR A PLACA DO VEÍCULO")
        Else
            REVISAOTableAdapter.FillByPLACA(REVISAODataSet.REVISAO, PLACAMaskedTextBox.Text)
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If STATUSComboBox.Text = "" Then
            MsgBox("FAVOR INFORMAR O STATUS PARA PESQUISA")
        Else
            REVISAOTableAdapter.FillBySTATUS(REVISAODataSet.REVISAO, STATUSComboBox.Text)
        End If
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If AGENDAMENTOMaskedTextBox.Text = "  /  /       :" Then
            MsgBox("FAVOR INFORMAR A DATA E HORA DO AGENDAMENTO")
        Else
            REVISAOTableAdapter.FillByDIA(REVISAODataSet.REVISAO, AGENDAMENTOMaskedTextBox.Text, DateValue(AGENDAMENTOMaskedTextBox.Text).AddDays(1))
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        REVISAOTableAdapter.Fill(REVISAODataSet.REVISAO)
    End Sub
End Class